function calculateCarbon() {
    const distance = parseFloat(document.getElementById('distance').value);
    const publicTransport = parseFloat(document.getElementById('publicTransport').value);
    const meatConsumption = parseFloat(document.getElementById('meatConsumption').value);
  
    // 假设的系数
    const distanceFactor = 0.2; // 每公里产生的碳排放量
    const publicTransportFactor = -0.1; // 每次使用公共交通减少的碳排放量
    const meatFactor = 2.3; // 每公斤肉类产生的碳排放量
  
    // 计算碳排放
    const carbonEmission = (distance * distanceFactor) 
                         + (publicTransport * publicTransportFactor) 
                         + (meatConsumption * meatFactor);
  
    // 显示结果
    const resultDiv = document.getElementById('result');
    resultDiv.innerHTML = `<strong>您的碳排放量约为：${carbonEmission.toFixed(2)} kg CO2</strong>`;
  
    // 保存结果
    saveResult(carbonEmission);
  }
  
  function saveResult(emission) {
    const results = JSON.parse(localStorage.getItem('carbonResults')) || [];
    const timestamp = new Date().toISOString();
    results.push({ timestamp, emission });
    localStorage.setItem('carbonResults', JSON.stringify(results));
  }
  
  document.getElementById('viewResults').addEventListener('click', function() {
    const results = JSON.parse(localStorage.getItem('carbonResults')) || [];
    results.sort((a, b) => a.timestamp.localeCompare(b.timestamp)); // 按时间排序
  
    const resultsList = document.getElementById('resultsList');
    resultsList.innerHTML = results.map(result => 
      `<div><strong>${new Date(result.timestamp).toLocaleString()}</strong>:${result.emission.toFixed(2)} kg CO2</div>`
    ).join('');
  });
  